#include "rc.h"
#include <string.h>
#include <uprocess.h>
#include <ustdio.h>
#include <stdio.h>
int creadline(int fd,char *buf){
	int ptr = 0;
	char buf2 =0;
	while(1){
		int read = uread(fd,(uint8_t*) &buf2,1);
		if (read != 1)
			return -1;//TODO:Bug handler
		if (buf2 == 0xD)
			continue;
		if (buf2 == 0xA){
			*(buf+ptr) = 0;
			return 0;
		}
		*(buf+ptr) = buf2;
		ptr+=read;		
	}
}

int exec_rc(char *file,rc_callback_t cb){
	int rcfile = uopen(file,0,0);
	char buffer[80];
	if (rcfile == -1)
		return -1;
	while (1){
		if (creadline(rcfile,buffer) != 0)
			break;
		if (strlen(buffer) == 0)
			break;
		if (strcmp(buffer,"END") == 0)
			break;
		if (!cb(buffer))
			return -2;
	}
	uclose(rcfile);
	return 0;
}